package receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by eyesee on 2017/6/16.
 */

public class UsbReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Uri uri = intent.getData();
        String path = uri.getPath();
        if (action.equals(Intent.ACTION_MEDIA_MOUNTED)){
            /**收到广播后发送消息到EventBus*/
            EventBus.getDefault().post(new UsbEvent("收到"));

            if (path.contains("USB_DISK2")){
                Log.i("eyesee", "onReceive:得到一个插入的U盘路径,U盘1:"+ path +"/udisk0");
            }
            if (path.contains("USB_DISK3")){
                Log.i("eyesee", "onReceive:得到一个插入的U盘路径,U盘2:"+ path +"/udisk0");
            }
        }

        if (action.equals(Intent.ACTION_MEDIA_UNMOUNTED)){
            /**收到广播后发送消息到EventBus*/
            EventBus.getDefault().post(new UsbEvent("取消"));

            if (!path.contains("USB_DISK2")){
                Log.i("eyesee", "onReceive:udisk2拔出,剩下="+ path +"/udisk0");
            }
            if (!path.contains("USB_DISK3")){
                Log.i("eyesee", "onReceive:udisk3拔出,剩下="+ path +"/udisk0");
            }
        }
    }

    public class UsbEvent {
        private final String mText;

        public UsbEvent(String text) {
            mText = text;
        }

        public String getText(){
            return mText;
        }
    }
}
